﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RobotBattle2.Scripting;
using MbUnit.Framework;

namespace Tests.RobotBattle2.Rsl2 {
	public static class AstUtils {
		public static void AssertVariable(this Expression expression, string name) {
			AssertVariable(expression, name, "Did not find the expected variable");
		}
		public static void AssertVariable(this Expression expression, string name, string message) {
			Assert.IsInstanceOfType(typeof(VariableExpression), expression, message);
			var varExpr = (VariableExpression)expression;
			Assert.AreEqual(name, varExpr.Symbol.Text, message);
		}
		public static T As<T>(this Expression expression) where T : Expression {
			Assert.IsInstanceOfType(typeof(T), expression);
			return (T)expression;
		}

		public static T As<T>(this Statement expression) where T : Statement {
			Assert.IsInstanceOfType(typeof(T), expression);
			return (T)expression;
		}

		public static BinaryExpression AsBinary(this Expression expression, BinaryOperator @operator) {
			var binary = expression.As<BinaryExpression>();
			Assert.AreEqual(@operator, binary.Operator);
			return binary;
		}

		public static Expression AsExpression(this Statement statement) {
			Assert.IsInstanceOfType(typeof(ExpressionStatement), statement);
			return ((ExpressionStatement)statement).Expression;
		}
	}
}
